﻿using MovieTheaterApp.DataModels;
using MovieTheaterApp.Db;
using MovieTheaterApp.Db.IDataAdapters.Adapters;
using MovieTheaterApp.Db.IDataAdapters.Interfaces;
using MovieTheatherApp.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MovieTheatherApp.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        //Declaring local instance of an adapter
        IMovieAdapter _adapter;
        //Declare the Home Controller Constructor
        //This is the default
        public HomeController()
        {
            _adapter = new MovieAdapter();
        }
        //Overload for Testing
        public HomeController(IMovieAdapter adapter)
        {
            _adapter = adapter;
        }
        [AllowAnonymous]
        public ActionResult Index()
        {

            List<Movie> movies = _adapter.GetAllMovies();
            return View(movies);
        }

        public ActionResult Details(int id)
        {
            MovieDetailsVM model = new MovieDetailsVM();
            model.Movie = _adapter.GetMovie(id);
            model.Actors = _adapter.GetActorsForMovie(id);
            
            return View(model);
        }
        [HttpGet]
        public ActionResult AddMovie()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddMovie(string title, string poster, Rating rating)
        {
            Movie movie = new Movie();
            movie.Title = title;
            movie.Poster = poster;
            movie.Rating = rating;
            movie = _adapter.AddMovie(movie);
            return RedirectToAction("Details/" + movie.Id);
        }
        [HttpGet]
        public ActionResult UpdateMovie(int id)
        {
            Movie movie;
            movie = _adapter.GetMovie(id);
            return View(movie);
        }
        [HttpPost]
        //public ActionResult UpdateMovie(string title, string poster, Rating rating, int id)
        public ActionResult UpdateMovie(Movie movie)
        {
            movie = _adapter.UpdateMovie(movie); 
            return RedirectToAction("Details/" + movie.Id);
        }
        public ActionResult AddReview(int id)
        {

            return View(id);
        }
        [HttpPost]
        public ActionResult AddReview(int id, string title, string comment)
        {
            Review rev = new Review();
            rev.Title = title;
            rev.Comment = comment;
            rev.MovieId = id;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Reviews.Add(rev);
                db.SaveChanges();
            }

            return RedirectToAction("Details/" + id);
        }
        public ActionResult DeleteReview(int id)
        {
            Review review;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                review = db.Reviews.Find(id);
                db.Reviews.Remove(review);
                db.SaveChanges();
            }
            return RedirectToAction("AddReview", new { id = review.MovieId });
        }

        [HttpPost]
        public ActionResult DeleteMovie(int id)
        {
            _adapter.DeleteMovie(id);
            return RedirectToAction("Index");
        }
    }
}